﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
namespace UI.WebControls.ControlsBase
{
    /// <summary>
    /// UI层，页面控件基类，多选列表基类
    /// </summary>
    public class CheckBoxListBase : CheckBoxList
    {
        /// <summary>
        /// 默认选中的多选集合
        /// </summary>
        public List<string> DefaultSelect = new List<string>();
        /// <summary>
        /// 重载基类事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.RepeatDirection = RepeatDirection.Horizontal;
            base.RepeatLayout = RepeatLayout.Flow;
            if (DefaultSelect != null && DefaultSelect.Count > 0)
                this.SetSelectValue();
            base.OnLoad(e);
        }
        /// <summary>
        /// 设置默认选中的多选项
        /// </summary>
        private void SetSelectValue() 
        {
            foreach (ListItem item in this.Items) 
                foreach (string val in DefaultSelect)
                    if (item.Value.Equals(val))
                        item.Selected = true;
        }
        /// <summary>
        /// 获取选中的功能
        /// </summary>
        /// <returns></returns>
        public string GetSelectValue()
        {
            string selVal = string.Empty;
            foreach (ListItem item in this.Items)
            {
                if (item.Selected)
                {
                    if (string.IsNullOrEmpty(selVal))
                        selVal = item.Value;
                    else
                        selVal += "," + item.Value;
                }
            }
            return selVal;
        }
    }
}
